package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Service("workflowOperatorComboHandlerImpl")
public class WorkflowOperatorComboHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.RADIO_GROUP.getType(), FieldTypeEnum.COMBO.getType());
    }

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        String corpid = workflowDataPojo.getCorpid();
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, workflowOperationsPojo.getBusinessType(), corpid, workflowOperationsPojo.getFormId());
            fieldAttrEntity.setItems(itemPoJos);
        }
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo,
                                         WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        Object object = workflowDataPojo.getValue();
        if (Objects.isNull(object)) {
            return null;
        }
        String corpid = workflowDataPojo.getCorpid();
        Integer businessType = workflowNodeEntity.getBusinessType();
        List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, businessType, corpid, workflowNodeEntity.getFormId());
        targetField.setItems(itemPoJos);
        return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType()) && Objects.nonNull(workflowOperationsPojo.getValue())) {
            Object object = workflowOperationsPojo.getValue();
            if (object instanceof String) {
                String corpid = workflowDataPojo.getCorpid();
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, workflowNodeEntity.getBusinessType(), corpid, workflowNodeEntity.getFormId());
                targetField.setItems(itemPoJos);
                object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
                if (Objects.isNull(object)) {
                    // 避免不存在的值去更新数据
                    return null;
                }
            } else if (object instanceof Map || object instanceof ItemPoJo) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                object = itemPoJo.getValue();
            }
            return object;
        }
        return workflowOperationsPojo.getValue();
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType,  String corpid) throws XbbException {
        Object comboValue = "";
        // 1、如果本表单字段是下拉框、下拉复选、单选按钮、复选按钮，需要先根据value取出text，放入data中；
        // 2、在下方取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, businessType, corpid, formId);
            fieldAttrEntity.setItems(itemPoJos);
            comboValue = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        } else {
            comboValue = data.get(fieldAttrEntity.getAttr());
        }
        if (Objects.isNull(comboValue)) {
            comboValue = "";
        }
        Object value;
        if (comboValue instanceof List) {
            value = JSON.toJSONString(comboValue);
        } else {
            value = comboValue;
        }
        return "\""+ value + "\"";
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        //  接上面的第2点在这里取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, workflowNodeEntity.getBusinessType(), corpid, workflowNodeEntity.getFormId());
            targetField.setItems(itemPoJos);
            return ItemUtil.getCustomItemValue(targetField, value.toString());
        } else {
            return value;
        }
    }
}
